package com.online.fund.spider;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.online.fund.util.HttpClientUtil;

/**
 * 基金行业配置
 * @author galen.zhang
 *
 */
public class FundIndustryConfigurationPageProcesser {

	private final static String CALLBACK_NAME = "jQuery18305391297509972106_";
	private final static String FUND_PAGE_URL = "http://api.fund.eastmoney.com/f10/HYPZ/?fundCode=#{CODE}&year=&callback=" + CALLBACK_NAME;// + "&_=1516531780567";
	
	private final static Logger logger = LoggerFactory.getLogger(FundIndustryConfigurationPageProcesser.class);
	
	public void parsePage(String fundCode, String fundName) {
		try {
			System.out.println();//http://api.fund.eastmoney.com/f10/HYPZ/?fundCode=000083&year=&callback=jQuery18307260889936513992_1516533904864&_=1516533905390
//			String resultStr = HttpClientUtil.httpGetRequest(FUND_PAGE_URL.replace("#{CODE}", fundCode));
			long currentTime = System.currentTimeMillis();
			String url = FUND_PAGE_URL.replace("#{CODE}", fundCode) + currentTime + "&_=" + System.currentTimeMillis();
			System.out.println(url);
//			String resultStr = HttpClientUtil.httpGetRequest(url);
//			String resultStr = HttpClientUtil.httpGetRequest("http://api.fund.eastmoney.com/f10/HYPZ/?fundCode=000083&year=&callback=jQuery18307260889936513992_1516533904864&_=1516533905390");
//			resultStr = resultStr.replace(CALLBACK_NAME + "(", "");
//			resultStr = resultStr.substring(0, resultStr.length() - 1);
//			System.out.println(resultStr);
			CookieStore cookieStore = new BasicCookieStore();  
		    CloseableHttpClient httpClient = HttpClients.custom()  
		             .setDefaultCookieStore(cookieStore)  
		             .build(); 
			try {

				HttpGet get = new HttpGet(url);
				get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
				BasicClientCookie cookie = new BasicClientCookie("st_pvi", "32400501825009");
				cookieStore.addCookie(cookie);
				cookie.setDomain("api.fund.eastmoney.com");
				cookie.setPath("/"); 
				BasicClientCookie cookie1 = new BasicClientCookie("qgqp_b_id", "2953587e40867b707d47fd115966ecb6");
				cookieStore.addCookie(cookie1);
				cookie1.setDomain("api.fund.eastmoney.com");
				cookie1.setPath("/"); 
				BasicClientCookie cookie2 = new BasicClientCookie("st_si", "79038458658342");
				cookieStore.addCookie(cookie2);
				cookie2.setDomain("api.fund.eastmoney.com");
				cookie2.setPath("/"); 
				BasicClientCookie cookie3 = new BasicClientCookie("em_hq_fls", "js");
				cookie3.setDomain("api.fund.eastmoney.com");
				cookieStore.addCookie(cookie3);
				cookie3.setPath("/"); 
				BasicClientCookie cookie4 = new BasicClientCookie("HAList", "a-sh-600030-%u4E2D%u4FE1%u8BC1%u5238%2Ca-sh-601788-%u5149%u5927%u8BC1%u5238");
				cookie4.setDomain("api.fund.eastmoney.com");
				cookieStore.addCookie(cookie4);
				cookie4.setPath("/"); 
				
				CloseableHttpResponse response = httpClient.execute(get);//
//				List<Cookie> cookies = cookieStore.getCookies();
//				for (int i = 0; i < cookies.size(); i++) {
//					System.out.println("Local cookie: " + cookies.get(i));
//				}
				HttpEntity entity = response.getEntity();
		        if (entity != null) {
		            // long len = entity.getContentLength();// -1 表示长度未知
		            String result = EntityUtils.toString(entity, "UTF-8");
		            System.out.println(result);
		            response.close();
		            // httpClient.close();
		        }
			} catch (Exception e) {
				e.printStackTrace();
			} finally {

			} 
			
			
		} catch (Exception e) {
			logger.error(fundCode, e);
		}
		
	}
	
	public static void main(String[] args) throws Exception {
		FundIndustryConfigurationPageProcesser processer = new FundIndustryConfigurationPageProcesser();
		processer.parsePage("000083", "汇添富消费行业混合");
	}
}
